<template>
	<view class="bg-white">
		<view class="padding">
			<view class="text-bold text-lg margin-bottom">书评</view>
			<uni-forms :value="reading_notes" ref="form">
				<uni-forms-item >
					<uni-rate class="flex justify-center" :size="24" :value="reading_notes.rate"/>
				</uni-forms-item>
				<uni-forms-item name="text">
					<uni-easyinput type="textarea" autoHeight v-model="reading_notes.text" placeholder="写几句读后感吧" />
				</uni-forms-item>
			</uni-forms>
		</view>
		<!-- 提交按钮 -->
		<view class="cu-bar btn-group foot">
			<button class="cu-btn bg-blue shadow-blur round lg main-btn" @click="submit">提交</button>
		</view>
	</view>
</template>

<script>
	const db = uniCloud.database();
	const app = getApp();
	export default {
		data() {
			return {
				book_id: null,
				reading_notes: {
					rate: 5,
					text: ""
				},
				userInfo: null,
				book_data: null
			}
		},
		onLoad: function(e) {
			this.book_id = e.book_id;
			db.collection("library-books").where("book_id=='" + this.book_id + "'").get().then(res => {
				this.book_data = res.result.data[0]
			})
			uni.setNavigationBarTitle({
				title: e.title
			})
			db.collection("uni-id-users").doc(app.globalData.uid).field('avatar, nickname, name').get().then(res => {
				this.userInfo = res.result.data[0]
			})
		},
		methods: {
			submit: function(e) {
				uni.showLoading({
					title: "上传中……"
				})
				db.collection("library-reading-notes").add({
					book_id: this.book_id,
					nickname: this.userInfo.nickname,
					avatar: this.userInfo.avatar,
					reading_notes: this.reading_notes.text,
					rate: this.reading_notes.rate,
					book_title: this.book_data.title,
					name: this.userInfo.name
				}).then(() => {
					uni.hideLoading();
					uni.showToast({
						title: "提交成功"
					})
					
				})
			}
		}
	}
</script>

<style>
	.cu-bar.foot {
			position: fixed;
			width: 100%;
			bottom: 50rpx;
			z-index: 1024;
			box-shadow: 0 -1upx 6upx rgba(0, 0, 0, 0);
		}

</style>
